﻿using FastDFSCore;
using HelenServer.FileStorage.Contracts;

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddDistributedFileStorage(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddFastDFSCore(cfg =>
        {
            var config = configuration.GetSection("FastDFS");
            var name = config.GetValue<string>("name");
            var trackers = new List<Tracker>()
            {
                new Tracker
                {
                    IPAddress = config.GetSection("IPAddress").Value,
                    Port = int.Parse(config.GetSection("Port").Value)
                }
            };

            cfg.ClusterConfigurations.Add(new ClusterConfiguration
            {
                Name = name,
                Trackers = trackers,
            });
        }).AddFastDFSDotNetty();

        services.AddOptions<FastDFSContainerOptions>()
            .Configure<IConfiguration>((opts, cfg) =>
            {
                cfg.Bind(FastDFSContainerOptions.Position, opts);
            });

        return services;
    }
}